/*
 * @Author: liu
 * @Date:   2018-03-15
 * +----------------------------------------------------------------------
 * | admin [ panel页公共js ]
 * | 存储session
 * +----------------------------------------------------------------------
 */
layui.define(['jquery', 'form', 'table', 'configureCom', 'configure', 'bluebird'], function (exports) {
    var form = layui.form;
    var table = layui.table;
    var $ = layui.jquery;
    window.configureCom = layui.configureCom;
    window.configure = layui.configure;
    var panel = {
        dbInit: function () {//初始化代办事项
            var htmlHead = window.location.href.split('pages')[0];
            $('.dbBox').html('');
            var dbUrl = configure.GET_TODO_WORK_HTTP;
            configureCom.getAjax(dbUrl, {}).then(function (res) {
                var data = res.result;
                window.toDaiBan = function (i) {
                    top.toDaiBan(data[i]);
                };
                $.each(data, function (index, item) {
                    item.html = htmlHead + item.html;
                    var dbHtml = '<li class="layui-col-xs3">' +
                        '                        <a onclick="toDaiBan(' + index + ')" class="layadmin-backlog-body" style="cursor: pointer;">' +
                        '                            <h3>' + item.name + '</h3>' +
                        '                            <p><cite class="cite' + index + '">0</cite></p>' +
                        '                        </a>' +
                        '                    </li>';
                    $('.dbBox').append(dbHtml);
                    configureCom.getAjax(item.http, {}).then(function (res) {//获取待办条数
                        var num = res.result.totalElements;
                        $('.cite' + index).html(num);
                    });
                });
            });
        },
        noticeInit: function () {
            var cols = [[ //表头
                {
                    title: '通知标题', templet: function (d) {
                        var html = d.noticeTitle;
                        return html;
                    }
                },
                {
                    title: '发布时间', width: 170, templet: function (d) {
                        return configureCom.dateToStrAll(d.createdDate);
                    }
                }
            ]];
            var url = configure.NOTICE_LOGIN_LIST_HTTP;
            var data = {};
            configureCom.tableRender(url, data, cols, false, false, '470');
            table.on('row(klmyTable)', function (obj) {// 双击选中
                var url = configure.NOTICE_DETAIL_ONLY_HTML + '?noticeId=' + obj.data.noticeId;
                var title = '查看';
                configureCom.klmyPop([url, title]);
            });
        },
        getEchartData: function (jgjgData) {
            //获取业绩图标的data
            return new Promise(function (resolve, reject) {
                if (jgjgData) {
                    resolve({result: jgjgData});
                } else {
                    configureCom.getAjax(configure.ACHIEVEMENT_LOGIN_PAGE_HTTP, {}).then(function (res) {
                        resolve(res);
                    }, function (err) {
                        reject(err);
                    });
                }
            });
        },
        chartInit: function (initData, jgjgData) {
            var myChart = echarts.init(document.getElementById('main'));
            var yearArr = [];//年份
            var currentIndex = 0;//当前年份
            var rightTopArr = [];//右上角项
            var rightTopType = [];//右上角项
            var xAxisArr = initData.xAxisArr;//横轴
            var yAxisArr = [];//纵轴
            var rightTop = initData.rightTop;
            for (var i = 0; i < rightTop.length; i++) {
                var item = rightTop[i];
                rightTopArr.push(item.name);
                rightTopType.push(item.type);
                if (i < 2) {
                    yAxisArr.push({
                        'type': 'value',
                        'name': item.name + '（' + item.unit + '）'
                        // ,'max': 60000
                    });
                }
            }
            panel.getEchartData(jgjgData).then(function (res) {
                var resData = res.result;
                if (resData.length == 0) {
                    return false;
                }

                $.each(resData, function (index, item) {
                    if (yearArr.indexOf(item.year) == -1) {
                        yearArr.push(item.year);
                    }
                });
                yearArr = yearArr.sort();

                $.each(yearArr, function (index, item) {
                    var newYear = new Date().getFullYear();
                    if (item == newYear) {
                        currentIndex = index;
                    }
                });

                var getOneArr = function (year, name) {//获取某一组数据
                    var arr = [];
                    for (var i = 0; i < xAxisArr.length; i++) {
                        arr[i] = 0;
                    }
                    $.each(resData, function (index, item) {
                        if (item.year == year) {
                            $.each(xAxisArr, function (s, k) {
                                if (k == item.projTypeName) {
                                    arr[s] = item[name] || 0;
                                }
                            });
                        }
                    });
                    return arr;
                };

                var option = {
                    timeline: {
                        data: yearArr,
                        autoPlay: false,//是否自动跳转
                        playInterval: 2000,//跳转间隔
                        currentIndex: currentIndex//当前
                    },
                    options: [
                        {
                            title: {
                                'text': yearArr[0] + '年',
                                'subtext': ''
                            },
                            tooltip: {'trigger': 'axis'},
                            legend: {
                                x: 'right',
                                'data': rightTopArr
                            },
                            toolbox: {
                                'show': true,
                                orient: 'vertical',
                                x: 'right',
                                y: 'center',
                                'feature': {
                                    'mark': {'show': true},
                                    'dataView': {'show': true, 'readOnly': false},
                                    'magicType': {'show': true, 'type': ['line', 'bar', 'stack', 'tiled']},
                                    'restore': {'show': true},
                                    'saveAsImage': {'show': true}
                                }
                            },
                            calculable: true,
                            grid: {'y': 80, 'y2': 100},
                            xAxis: [{
                                'type': 'category',
                                'axisLabel': {'interval': 0},
                                'data': xAxisArr
                            }],
                            yAxis: yAxisArr,
                            series: [
                                {
                                    'name': rightTopArr[0],
                                    'type': 'bar',
                                    'markLine': {
                                        symbol: ['arrow', 'none'],
                                        symbolSize: [4, 2],
                                        itemStyle: {
                                            normal: {
                                                lineStyle: {color: 'orange'},
                                                barBorderColor: 'orange',
                                                label: {
                                                    position: 'left',
                                                    formatter: function (params) {
                                                        return Math.round(params.value);
                                                    },
                                                    textStyle: {color: 'orange'}
                                                }
                                            }
                                        },
                                        'data': [{'type': 'average', 'name': '平均值'}]
                                    },
                                    'data': getOneArr(yearArr[0], rightTopType[0])
                                }
                                // ,{
                                //     'name': rightTopArr[1], 'yAxisIndex': 1, 'type': 'bar',
                                //     'data': getOneArr(yearArr[0], rightTopType[1])
                                // }
                            ]
                        }
                    ]
                };
                if (rightTopArr.length > 1) {
                    for (var i = 1; i < rightTopArr.length; i++) {
                        option.options[0].series.push({
                            name: rightTopArr[i],
                            yAxisIndex: 1,
                            type: 'bar',
                            data: getOneArr(yearArr[0], rightTopType[i])
                        });
                    }
                }

                if (yearArr.length > 1) {
                    for (var j = 1; j < yearArr.length; j++) {
                        var seriesArr = [];
                        for (var s = 0; s < rightTopType.length; s++) {
                            var obj = {
                                data: getOneArr(yearArr[j], rightTopType[s])
                            };
                            seriesArr.push(obj);
                        }
                        option.options.push(
                            {
                                title: {'text': yearArr[j] + '年'},
                                series: seriesArr
                            }
                        );
                    }
                }
                myChart.setOption(option);
            });
        }
    };
    exports('panel', panel);
});